package com.china08.yxyapi.api.manage.tec.teaching.homework.uploadques;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.question.QuestionUploadKnowModel;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkQuestionUploadService;
import com.china08.yxyapi.util.SpringContextUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class homeworkUploadJieDaController extends YxyApiBaseController {

    @Autowired
    private HomeworkQuestionUploadService homeworkQuestionUploadService;

    @ApiOperation(value = "试题上传")
    @RequestMapping(value = "/api_yw/manage/tec/homework/question/jieda/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody QuestionJieDaUploadModel reqModel) throws JsonProcessingException {
        String userId = getProfile(request, response).getId();
        System.out.println("解答题");
        reqModel.userId = userId;
        RespModel model = new RespModel();
        model.id = homeworkQuestionUploadService.uploadJieDa(reqModel);
        return model;
    }

    public static class ChooseModel {
        public String choose;
        public String body;
    }

    public static class QuestionJieDaUploadModel extends YxyApiReqModel {
        public String id;
        public String questionType;
        public String srcType;
        public String difficultyLevel;
        public List<QuestionUploadKnowModel> knowList;
        public String tigan;
        public String daan;
        public String jiexi;
        public String phase;
        public String subject;
        public String userId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(tigan.trim(), null), "题干不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(daan.trim(), null), "答案不能为空");

            Assert.notNull(StringUtils.defaultIfBlank(questionType, null), "请选择题型。");
            Assert.isTrue(StringUtils.equals(questionType, "6"), "此模版只能上传解答题");
            Assert.notNull(knowList, "请选择知识点");
            Assert.isTrue(knowList.size() > 0, "请选择知识点");
            if (StringUtils.isBlank(id)) {
                Assert.notNull(StringUtils.defaultIfBlank(phase, null), "找不到学段请刷新页面。");
                Assert.notNull(StringUtils.defaultIfBlank(subject, null), "找不到学段请刷新页面。");
            } else {
                ElasticYxyYxyQuesResource elasticYxyYxyQuesResource = SpringContextUtil
                        .getBean(ElasticQuesService.class).findByEsQuesId(id);
                Assert.notNull(elasticYxyYxyQuesResource, "找不到试题，请刷新页面");
                this.phase = elasticYxyYxyQuesResource.getPhase();
                this.subject = elasticYxyYxyQuesResource.getSubject();
            }
        }
    }

    protected static class RespModel {
        public String id;
    }
}
